package com.hanserwei.consumer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hanserwei.domain.dataobject.Orders;
import com.hanserwei.service.OrdersService;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class RabbitMQListener {

    @Resource
    private OrdersService ordersService;

    @RabbitListener(queues = "order.queue.one")
    @SneakyThrows
    public void receiveOne(String message) {
        // 字符串转为对象
        ObjectMapper objectMapper = new ObjectMapper();
        Orders orders = objectMapper.readValue(message, Orders.class);
        boolean order = ordersService.createOrder2(orders);
        log.info("队列1创建订单：{}", order);
    }

    @RabbitListener(queues = "order.queue.two")
    @SneakyThrows
    public void receiveTwo(String message) {
        // 字符串转为对象
        ObjectMapper objectMapper = new ObjectMapper();
        Orders orders = objectMapper.readValue(message, Orders.class);
        boolean order = ordersService.createOrder2(orders);
        log.info("队列2创建订单：{}", order);
    }
}
